import psycopg2

class User:
    def __init__(self, id, username, password):
        self.id = id
        self.username = username
        self.password = password
        # 由于数据库中没有 is_active 字段，默认设置为 True
        self._is_active = True

    def is_authenticated(self):
        return True

    def is_active(self):
        return self._is_active

    def is_anonymous(self):
        return False

    def get_id(self):
        return str(self.id)

class PostgreSQLWrapper:
    def __init__(self, database="tianyan", user="postgres",password="520520",host="localhost", port="5432"):
        self.conn = psycopg2.connect(
            host=host,
            port=int(port),
            database=database,
            user=user,
            password='520520',  # 移除 .encode('utf-8')
            client_encoding='utf8'
        )
        print(self.conn)
        self.cur = self.conn.cursor()

    def execute_query(self, query):
        self.cur.execute(query)
        rows = self.cur.fetchall()
        return rows

    def execute_insert(self, query):
        self.cur.execute(query)
        self.conn.commit()

    def execute_update(self, query):
        self.cur.execute(query)
        self.conn.commit()

    def execute_delete(self, query):
        self.cur.execute(query)
        self.conn.commit()

    def close(self):
        self.cur.close()
        self.conn.close()

    def get_user_by_id(self, user_id):
        query = "SELECT * FROM users WHERE id = %s"
        self.cur.execute(query, (user_id,))
        user_data = self.cur.fetchone()
        if user_data and len(user_data) >= 0:
            return User(user_data[0], user_data[1], user_data[2])
        return None


    def authenticate_user(self, username, password):
        query = "SELECT * FROM users WHERE username = %s AND password = %s"
        self.cur.execute(query, (username, password))
        user_data = self.cur.fetchone()
        print(user_data)
        if user_data:
            user = User(user_data[0], user_data[1], user_data[2])
            print("111",user)
            return user
        else:
            print("222")
        return None
    def add_ip_config(self,line_type, ip_address, test_software, product_type):
        try:
            query = "INSERT INTO ip_configs (line_type, ip_address, test_software, product_type) VALUES (%s, %s,%s,%s)"
            self.cur.execute(query, (line_type, ip_address, test_software, product_type))
            self.conn.commit()
            return True
        except psycopg2.IntegrityError:
           return False
    def get_top_100_ip_configs(self):
        query = "SELECT * FROM ip_configs LIMIT 100"
        self.cur.execute(query)
        return self.cur.fetchall()

    def verify_invite_code(self, code):
        query = "SELECT * FROM invite_codes WHERE code = %s"
        self.cur.execute(query, (code,))
        return self.cur.fetchone() is not None

    def create_user(self, username, password):
        query = "SELECT * FROM users WHERE username = %s"
        self.cur.execute(query, (username,))
        if self.cur.fetchone():
            return False
        query = "INSERT INTO users (username, password) VALUES (%s, %s)"
        self.cur.execute(query, (username, password))
        self.conn.commit()
        return True

    def update_ip_config(self, id, line, address, software, product_type):
        query = "UPDATE ip_configs SET line_type = %s, ip_address = %s, test_software = %s, product_type = %s WHERE id = %s"
        self.cur.execute(query, (line, address, software, product_type, id))
        self.conn.commit()
        return True

    def delete_ip_config(self, id):
        query = "DELETE FROM ip_configs WHERE id = %s"
        self.cur.execute(query, (id,))
        self.conn.commit()
        return True

    def is_line_type_unique(self, line_type, exclude_id=None):
        query = "SELECT * FROM ip_configs WHERE line_type = %s"
        if exclude_id:
            query += " AND id != %s"
            self.cur.execute(query, (line_type, exclude_id))
        else:
            self.cur.execute(query, (line_type,))
        return self.cur.fetchone() is None

    def is_ip_address_unique(self, ip_address, exclude_id=None):
        query = "SELECT * FROM ip_configs WHERE ip_address = %s"
        if exclude_id:
            query += " AND id != %s"
            self.cur.execute(query, (ip_address, exclude_id))
        else:
            self.cur.execute(query, (ip_address,))
        return self.cur.fetchone() is None

    def get_ip_config_by_id(self, id):
        query = "SELECT * FROM ip_configs WHERE id = %s"
        self.cur.execute(query, (id,))
        ip_data = self.cur.fetchone()
        if ip_data:
            return ip_data
        return None
